*------------------------------------------------------------------------------*
							***CENTROIDS***
*------------------------------------------------------------------------------*

use "${rawdata}centroids/kreise_geocodes.dta", clear

keep region_kkz kreis_name geocodehere_lat geocodehere_lon

rename region_kkz kreis_code
rename kreis_name Kreis
rename geocodehere_lat latitude_d
rename geocodehere_lon longitude_d

**Add coordinates
*Gottingen Landkreis after addition of Osterode am Harz
set obs `=_N+1'
replace kreis_code = 3159 if kreis_code == .
replace Kreis = "Göttingen, Landkreis" if kreis_code == 3159
replace latitude_d = 51.5 if kreis_code == 3159  // Source: https://geohack.toolforge.org/geohack.php?pagename=G%C3%B6ttingen_(district)&params=51.5_N_9.92_E_
replace longitude_d = 9.92 if kreis_code == 3159  // Source: https://geohack.toolforge.org/geohack.php?pagename=G%C3%B6ttingen_(district)&params=51.5_N_9.92_E_
*Hannover Landeshauptstadt
set obs `=_N+1'
replace kreis_code = 3241001 if kreis_code == .
replace Kreis = "Landeshauptstadt Hannover" if kreis_code == 3241001
replace latitude_d = 52.37906 if kreis_code == 3241001  // Source: municipalities' centroids dataset provided by Nico
replace longitude_d = 9.770388 if kreis_code == 3241001  // Source: municipalities' centroids dataset provided by Nico
*Saarbrucken Landeshauptstadt
set obs `=_N+1'
replace kreis_code = 10041100 if kreis_code == .
replace Kreis = "Landeshauptstadt Saarbrücken" if kreis_code == 10041100
replace latitude_d = 49.23926 if kreis_code == 10041100  // Source: municipalities' centroids dataset provided by Nico
replace longitude_d = 7.007008 if kreis_code == 10041100  // Source: municipalities' centroids dataset provided by Nico

**Add Lander
merge 1:m kreis_code using "${tempdata}kreise_elections.dta", keep(match) nogen  // muni_code_str "${data}prep/muni_elections.dta" 
keep kreis_code Kreis Land latitude_d longitude_d Land  //  muni_code_str muni_name
by Land kreis_code, sort: keep if _n == 1  // keep only one observation per province

*Save
qui compress
save "${tempdata}/kreise_centroids.dta", replace  // "${data}maps/muni_centroids.dta" 

*Create individual files for each Lander
local lander "Baden_Württemberg Bayern Berlin Brandenburg Bremen Hamburg Hessen Mecklenburg_Vorpommern Niedersachsen Nordrhein_Westfalen Rheinland_Pfalz Saarland Sachsen Sachsen_Anhalt Schleswig_Holstein Thüringen"

foreach l of local lander {
	preserve
		keep if Land == "`l'"  // Landtagswahlen
		bys kreis_code: keep if _n ==1  // muni_code_str 
		qui compress
		save "${tempdata}/`l'_centroids.dta", replace
	restore
} // l

*------------------------------------------------------------------------------*

clear

exit




